home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2008 February / MAC_easy_02_08.iso / Software / Mail Plug-ins / MailUnread_widget.dmg / MailUnreadWidget.wdgt / MailUnreadWidget.js < prev    next >
Encoding:
JavaScript  |  2005-06-02  |  12.2 KB  |  524 lines

  1. /* 
  2.     -- MailUnreadWidget.js
  3.     -- MailUnreadWidget
  4.     
  5.     -- Created by ?? ? on 05/05/25
  6.     -- Copyright (c) 2005 QueueSoft. All rights reserved.
  7. */
  8. var version = 'ver 1.3';
  9. var timerInterval = null;
  10. var subj_timerInterval = null;
  11. var subj_waitTimerInterval = null;
  12. var mailappStatus = false;
  13. var refreshSecond = 30 * 1000;
  14. var account_num = 0;
  15. var mailbox_num = 0;
  16. var unread_num = 0;
  17. var unread_idx = 1;
  18. var subj_animating = false;
  19.  
  20. var select_account = 0;
  21. var select_mailbox = 0;
  22. var subject_animate = 0;
  23.  
  24. function getLocalizedString (key)
  25. {
  26.     try {
  27.         return localizedStrings[key];
  28.     } catch (ex) {}
  29.  
  30.     return key;
  31. }
  32.  
  33. function onshow () {
  34. //    MailUnreadPlugin.logMessage("js:onshow");
  35.  
  36.     if (timerInterval == null) {
  37.         timerInterval = setInterval("refreshScreen();",refreshSecond);
  38.     }
  39.     subj_animating = false;
  40.     refreshScreen();
  41. }
  42.  
  43. function onhide () {
  44. //    MailUnreadPlugin.logMessage("js:onhide");
  45.  
  46.     if (timerInterval != null) {
  47.         // clear the timer
  48.         clearInterval(timerInterval);
  49.         timerInterval = null;
  50.     }
  51.     if (subj_timerInterval != null) {
  52.         // clear the timer
  53.         clearInterval(subj_timerInterval);
  54.         subj_timerInterval = null;
  55.     }
  56.     if (subj_waitTimerInterval != null) {
  57.         // clear the timer
  58.         clearInterval(subj_waitTimerInterval);
  59.         subj_waitTimerInterval = null;
  60.     }
  61. }
  62.  
  63. function startRefreshTimer()
  64. {
  65.     if ( timerInterval != null )
  66.     {
  67.         clearInterval( timerInterval );
  68.         timerInterval = null;
  69.     }
  70.     
  71.     timerInterval = setInterval("refreshScreen();",refreshSecond);
  72. }
  73.  
  74. function loaded()
  75. {
  76.  
  77.     document.getElementById("unread_icon1").style.display = 'none'
  78.     select_account = 1;
  79.     select_mailbox = 0;
  80. // restore saved account No
  81.     if (window.widget) {
  82.         var save_account = widget.preferenceForKey(createkey("account"));
  83.         if(save_account != null)
  84.             select_account = save_account;
  85.         else
  86.             select_account = 1;
  87.     }
  88. // restore saved mailbox No
  89.     if (window.widget) {
  90.         var save_mailbox = widget.preferenceForKey(createkey("mailbox"));
  91.         if(save_mailbox != null)
  92.             select_mailbox = save_mailbox;
  93.         else
  94.             select_mailbox = 0;
  95.     }
  96. // restore saved subject_animate
  97.     if (window.widget) {
  98.         var save_animate = widget.preferenceForKey(createkey("animate"));
  99.         if(save_animate != null)
  100.             subject_animate = save_animate;
  101.         else
  102.             subject_animate = 0;
  103.     }
  104.     MailUnreadPlugin.setAccount(select_account);
  105.     MailUnreadPlugin.setMailbox(select_mailbox);
  106.  
  107.     refreshScreen();
  108.  
  109.     startRefreshTimer();
  110.  
  111. }
  112.  
  113. function refreshScreen()
  114. {
  115. //    MailUnreadPlugin.logMessage("js:refreshScreen");
  116.  
  117.     CheckMailapp();
  118.     if(mailappStatus) {
  119.         if(account_num == 0) {
  120.             account_num = MailUnreadPlugin.getAccountCount();
  121.             mailbox_num = MailUnreadPlugin.getMailboxCount();
  122.             SetSelectAccount();
  123.             SetSelectMailbox();
  124.         }
  125.         document.getElementById("account-popup").style.visibility = 'visible';
  126.         document.getElementById("mailbox-popup").style.visibility = 'visible';
  127.         unread_num = MailUnreadPlugin.getUnreadCount();
  128.         GetSubject();
  129.     } else {
  130.         document.getElementById("unread_icon1").style.display = 'none';
  131.         document.getElementById("accountName").innerHTML = null;
  132.         document.getElementById("mailBoxName").innerHTML = null;
  133.         document.getElementById("subject1").innerHTML = null;
  134.         document.getElementById("account-popup").style.visibility = 'hidden';
  135.         document.getElementById("mailbox-popup").style.visibility = 'hidden';
  136.     }
  137. }
  138.  
  139. function CheckMailapp()
  140. {
  141. //    MailUnreadPlugin.logMessage("js:CheckMailapp");
  142.  
  143.     var run = MailUnreadPlugin.isRunning();
  144.     var result = document.getElementById("mailStatus");
  145.     if(run == 0) {
  146.         result.innerHTML = getLocalizedString('None');
  147.         mailappStatus = false;
  148.     } else {
  149.         result.innerHTML = getLocalizedString('Running');
  150.         mailappStatus = true;
  151.     }
  152. }
  153.  
  154. function GetSubject()
  155. {
  156. //    MailUnreadPlugin.logMessage("js:GetSubject");
  157.  
  158.     if(select_mailbox != 0) {
  159.         document.getElementById("mailBoxTitle").style.left = "230px";
  160.         document.getElementById("mailBoxName").style.left = "320px";
  161.     } else {
  162.         document.getElementById("mailBoxTitle").style.left = "380px";
  163.         document.getElementById("mailBoxName").style.left = "470px";
  164.     }
  165.     document.getElementById("accountName").innerHTML = MailUnreadPlugin.getAccountAtIndex(select_account);
  166.     document.getElementById("mailBoxName").innerHTML = MailUnreadPlugin.getMailboxAtIndex(select_mailbox);
  167.  
  168.     if(unread_num > 0) {
  169.         if(subject_animate == 1) {
  170.             document.getElementById("unread_icon1").style.display = 'block';
  171.             if( !subj_animating ) {
  172.                 document.getElementById("subject1").innerHTML = MailUnreadPlugin.getUnreadSubjectAtIndex(unread_idx);
  173.                 unreadSubjectShowWait();
  174.             }
  175.         } else {
  176.             if(MailUnreadPlugin.getUnreadAtIndex(1) == 1)
  177.                 document.getElementById("unread_icon1").style.display = 'block';
  178.             else
  179.                 document.getElementById("unread_icon1").style.display = 'none';
  180.             document.getElementById("subject1").innerHTML = MailUnreadPlugin.getSubjectAtIndex(1);
  181.         }
  182.     } else {
  183.         if (subj_waitTimerInterval != null)
  184.         {
  185.             clearInterval (subj_waitTimerInterval);
  186.             subj_waitTimerInterval  = null;
  187.         }
  188.         subj_animating = false;
  189.         document.getElementById ('subject1').style.top = '0px';
  190.         document.getElementById("unread_icon1").style.display = 'none';
  191.         document.getElementById("subject1").innerHTML = MailUnreadPlugin.getSubjectAtIndex(1);
  192.     }
  193. }
  194.  
  195. function unreadSubjectShowWait()
  196. {
  197. //    MailUnreadPlugin.logMessage("js:unreadSubjectShowWait");
  198.     if (subj_waitTimerInterval != null)
  199.     {
  200.         clearInterval (subj_waitTimerInterval);
  201.         subj_waitTimerInterval  = null;
  202.     }
  203.     if(unread_num > 0) {
  204.         subj_waitTimerInterval = setInterval("subj_hide();",5000);
  205.         subj_animating = true;
  206.     } else {
  207.         subj_animating = false;
  208.     }
  209. }
  210.  
  211. function SetSelectAccount()
  212. {
  213. //    MailUnreadPlugin.logMessage("js:SetSelectAccount");
  214.     var select = document.getElementById ('account-popup');
  215.     var c = account_num;
  216.     
  217.     for (var i = 1; i <= c; ++i)
  218.     {
  219.  
  220.         var element = document.createElement("option");
  221.         element.innerText = MailUnreadPlugin.getAccountAtIndex(i);
  222.         select.appendChild (element);
  223.     }
  224.  
  225. }
  226.  
  227. function SetSelectMailbox()
  228. {
  229. //    MailUnreadPlugin.logMessage("js:SetSelectMailbox");
  230.     var select = document.getElementById ('mailbox-popup');
  231.     var c = mailbox_num;
  232.     
  233.     for (var i = 0; i <= c; ++i)
  234.     {
  235.  
  236.         var element = document.createElement("option");
  237.         element.innerText = MailUnreadPlugin.getMailboxAtIndex(i);
  238.         select.appendChild (element);
  239.     }
  240.  
  241. }
  242.  
  243. function ChangeAccount(accountSelect)
  244. {
  245.     select_account = accountSelect.selectedIndex + 1;
  246.     MailUnreadPlugin.setAccount(select_account);
  247. // save select account No
  248.     widget.setPreferenceForKey(select_account, createkey("account"));
  249. }
  250.  
  251. function ChangeMailbox(mailboxSelect)
  252. {
  253.     select_mailbox = mailboxSelect.selectedIndex;
  254.     MailUnreadPlugin.setMailbox(select_mailbox);
  255. // save select mailbox No
  256.     widget.setPreferenceForKey(select_mailbox, createkey("mailbox"));
  257. }
  258.  
  259. function ChangeAnimate(animateCheck)
  260. {
  261.     var check = animateCheck.checked;
  262.     if(check) {
  263.         widget.setPreferenceForKey(1, createkey("animate"));
  264.         subject_animate = 1;
  265.     } else {
  266.         widget.setPreferenceForKey(0, createkey("animate"));
  267.         subject_animate = 0;
  268.     }
  269. }
  270.  
  271. function showbackside(event)
  272. {
  273.     var front = document.getElementById("front-panel");
  274.     var back = document.getElementById("back-panel");
  275.     
  276.     if (window.widget)
  277.         widget.prepareForTransition("ToBack");
  278.     onhide();
  279.     document.getElementById('version').innerHTML = version;
  280.     front.style.display="none";
  281.     back.style.display="block";
  282.     
  283.     if (window.widget)
  284.         setTimeout ('widget.performTransition();', 0);    
  285.  
  286.  
  287.     document.getElementById('fliprollie').style.display = 'none';
  288. }
  289.  
  290. function doneClicked() {
  291.     var front = document.getElementById("front-panel");
  292.     var back = document.getElementById("back-panel");
  293.  
  294.     if (window.widget)
  295.         widget.prepareForTransition("ToFront");
  296.  
  297.  
  298.     front.style.display="block";
  299.     back.style.display="none";
  300.  
  301.     setTimeout ('flipitback();', 0);
  302.     onshow();
  303.  
  304. }
  305.  
  306.  
  307. function flipitback()
  308. {
  309. //    updateTime(false);
  310.     if (window.widget)
  311.         setTimeout("widget.performTransition();", 0);
  312. }
  313.  
  314. var flipShown = false;
  315. var subjshown = false;
  316.  
  317. var animation = {duration:0, starttime:0, to:1.0, now:0.0, from:0.0, element:null, timer:null};
  318.  
  319. var subj_animation = {duration:0, starttime:0, to:1.0, now:0.0, from:0.0, element:null, timer:null};
  320.  
  321. //  Returns val if min < val < max
  322. //  Returns min if val <= min
  323. //  Returns max if val >= max
  324.  
  325. function limit_3 (val, min, max)
  326. {
  327.     return val < min ? min : (val > max ? max : val);
  328. }
  329.  
  330. function computeNextFloat (from, to, ease)
  331. {
  332.     return from + (to - from) * ease;
  333. }
  334.  
  335. function subj_animate()
  336. {
  337.     var T;
  338.     var ease;
  339.     var time = (new Date).getTime();
  340.         
  341.     
  342.     T = limit_3(time-subj_animation.starttime, 0, subj_animation.duration);
  343.     
  344.     if (T >= subj_animation.duration)
  345.     {
  346.         clearInterval (subj_animation.timer);
  347.         subj_animation.timer = null;
  348.         subj_animation.now = subj_animation.to;
  349.     }
  350.     else
  351.     {
  352.         ease = 0.5 - (0.5 * Math.cos(Math.PI * T / subj_animation.duration));
  353.         subj_animation.now = computeNextFloat (subj_animation.from, subj_animation.to, ease);
  354.     }
  355.  
  356.     subj_animation.element.style.top = subj_animation.now + "px";
  357. }
  358.  
  359. function subj_hide ()
  360. {
  361.     // fade in the flip widget
  362.     if (subj_animation.timer != null)
  363.     {
  364.         clearInterval (subj_animation.timer);
  365.         subj_animation.timer  = null;
  366.     }
  367.     
  368.     var starttime = (new Date).getTime() - 13; // set it back one frame
  369.     
  370.     subj_animation.duration = 500;
  371.     subj_animation.starttime = starttime;
  372.     subj_animation.element = document.getElementById ('subject1');
  373.     subj_animation.timer = setInterval ("subj_animate();", 13);
  374. //    subj_animation.from = subj_animation.now;
  375.     subj_animation.from = 0.0;
  376.     subj_animation.to = 20.0;
  377.     subj_animate();
  378.  
  379.     if (subj_waitTimerInterval != null)
  380.     {
  381.         clearInterval (subj_waitTimerInterval);
  382.         subj_waitTimerInterval  = null;
  383.     }
  384.     subj_waitTimerInterval = setInterval("subj_show();",1000);
  385.  
  386. }
  387.  
  388. function getNextSubjectIdx()
  389. {
  390.     unread_idx++;
  391.     if(unread_idx > unread_num) {
  392.         unread_idx = 1;
  393.     }
  394.     return unread_idx;
  395. }
  396.  
  397. function subj_show ()
  398. {
  399.     idx = getNextSubjectIdx();
  400.         document.getElementById("subject1").innerHTML = MailUnreadPlugin.getUnreadSubjectAtIndex(idx);
  401.  
  402.     // fade in the flip widget
  403.     if (subj_animation.timer != null)
  404.     {
  405.         clearInterval (subj_animation.timer);
  406.         subj_animation.timer  = null;
  407.     }
  408.     
  409.     var starttime = (new Date).getTime() - 13; // set it back one frame
  410.     
  411.     subj_animation.duration = 500;
  412.     subj_animation.starttime = starttime;
  413.     subj_animation.element = document.getElementById ('subject1');
  414.     subj_animation.timer = setInterval ("subj_animate();", 13);
  415.     subj_animation.from = -20.0;
  416.     subj_animation.to = 0.0;
  417.     subj_animate();
  418.  
  419.     unreadSubjectShowWait();
  420. }
  421.  
  422. function animate()
  423. {
  424.     var T;
  425.     var ease;
  426.     var time = (new Date).getTime();
  427.         
  428.     
  429.     T = limit_3(time-animation.starttime, 0, animation.duration);
  430.     
  431.     if (T >= animation.duration)
  432.     {
  433.         clearInterval (animation.timer);
  434.         animation.timer = null;
  435.         animation.now = animation.to;
  436.     }
  437.     else
  438.     {
  439.         ease = 0.5 - (0.5 * Math.cos(Math.PI * T / animation.duration));
  440.         animation.now = computeNextFloat (animation.from, animation.to, ease);
  441.     }
  442.     
  443.     animation.element.style.opacity = animation.now;
  444. }
  445.  
  446. function mousemove (event)
  447. {
  448.     if (!flipShown)
  449.     {
  450.         // fade in the flip widget
  451.         if (animation.timer != null)
  452.         {
  453.             clearInterval (animation.timer);
  454.             animation.timer  = null;
  455.         }
  456.         
  457.         var starttime = (new Date).getTime() - 13; // set it back one frame
  458.         
  459.         animation.duration = 500;
  460.         animation.starttime = starttime;
  461.         animation.element = document.getElementById ('flip');
  462.         animation.timer = setInterval ("animate();", 13);
  463.         animation.from = animation.now;
  464.         animation.to = 1.0;
  465.         animate();
  466.         flipShown = true;
  467.     }
  468. }
  469.  
  470. function mouseexit (event)
  471. {
  472.     if (flipShown)
  473.     {
  474.         // fade in the flip widget
  475.         if (animation.timer != null)
  476.         {
  477.             clearInterval (animation.timer);
  478.             animation.timer  = null;
  479.         }
  480.         
  481.         var starttime = (new Date).getTime() - 13; // set it back one frame
  482.         
  483.         animation.duration = 500;
  484.         animation.starttime = starttime;
  485.         animation.element = document.getElementById ('flip');
  486.         animation.timer = setInterval ("animate();", 13);
  487.         animation.from = animation.now;
  488.         animation.to = 0.0;
  489.         animate();
  490.         flipShown = false;
  491.     }
  492. }
  493.  
  494. function onremove ()
  495. {
  496.     if (window.widget)
  497.     {
  498.         widget.setPreferenceForKey (null, createkey("account"));
  499.         widget.setPreferenceForKey (null, createkey("mailbox"));
  500.     }
  501. }
  502.  
  503. if (window.widget)
  504. {
  505.     widget.onremove = onremove;
  506.     widget.onshow = onshow;
  507.     widget.onhide = onhide;
  508. }
  509.  
  510. function enterflip(event)
  511. {
  512.     document.getElementById('fliprollie').style.display = 'block';
  513. }
  514.  
  515. function exitflip(event)
  516. {
  517.     document.getElementById('fliprollie').style.display = 'none';
  518. }
  519.  
  520. function createkey(key)
  521. {
  522.     return widget.identifier + "-" + key;
  523. }
  524.